package com.intelligentmeterreadingsys.util;

import android.content.Context;
import android.content.Intent;
import android.location.LocationManager;
import android.provider.Settings;
import android.widget.Toast;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;


/**
 * 通过百度定位系统得到径维度
 * @author king
 *
 */
public class BaiDuGpsUtil {
	
	
	
	public LocationClient client;
    
	
	public  BDLocation location;
   
	public String Latitude;
	
	public String Longitude;
    
	public String getLatitude() {
		return Latitude;
	}

	public void setLatitude(String latitude) {
		Latitude = latitude;
	}

	public String getLongitude() {
		return Longitude;
	}

	public void setLongitude(String longitude) {
		Longitude = longitude;
	}

	public BaiDuGpsUtil(Context context){
		
	   client=new LocationClient(context);
		
	}
	
	/**
	 * 创建定位管理LocationClientOption，并设置定位参数，
	 * @return
	 */
	public static LocationClientOption getLocationClientOption(String type){
		  LocationClientOption option = new LocationClientOption();
	        //打开GPS
	        option.setOpenGps(true);
	        //设置坐标类型
	        option.setCoorType("bd09ll");	
	        
	        //设置网络优先
	        if(type.equals("gps")){
	        	  option.setPriority(LocationClientOption.GpsFirst);	
	        }else if(type.equals("network")){
	        	 option.setPriority(LocationClientOption.NetWorkFirst);
	        }
            
	       
	        
	        //设置产品线名称
	        option.setProdName("waterSys");
	        
	        //设置定位定时时间
	        if(type.equals("gps")){
	        	option.setScanSpan(10000);
	        }else{
	        	option.setScanSpan(5000);
	        }
	        //是否启用缓存定位
	        option.disableCache(true);
	        
			return option;
	               
	}
	
	
	/**
	 * 得到定位信息对象
	 * @return
	 */
	public BDLocation getBdLocation(){
		
		client.setLocOption(getLocationClientOption("network"));
		//设置监听器
		client.registerLocationListener(new BDLocationListener() {
			
			@Override
			public void onReceivePoi(BDLocation arg0) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void onReceiveLocation(BDLocation location) {
				getLoDoubles(location);
				
			}
		});
		return location;
		
	}
	
	/**
	 * 得到径维度
	 * @return
	 */
	public void getLoDoubles(BDLocation location){
		System.out.println("得到径维度");
	  if(location!=null){
		  this.Latitude=location.getLatitude()+"";
		  this.Longitude=location.getLongitude()+"";
	  }
		
		
	}
	
	
	/**
	 * 打开百度API
	 */
	public void start(){
		if(client!=null&&!client.isStarted()){
			client.start();
		}
	}
	
	/**
	 * 关闭百度API
	 */
	public void stop(){
		if(client!=null&&client.isStarted()){
			client.stop();
		}
	}
}
